package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizUploadScore;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizUploadScoreMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizUploadScoreParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizUploadScoreService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

/**
 * 网关上报分数表 服务实现类
 *
 * @author ssy
 * @date 2021/09/14 11:47
 */
@Service
public class BizUploadScoreServiceImpl extends ServiceImpl<BizUploadScoreMapper, BizUploadScore> implements BizUploadScoreService {

    @Override
    public PageResult<BizUploadScore> page(BizUploadScoreParam bizUploadScoreParam) {

        // 构造条件
        LambdaQueryWrapper<BizUploadScore> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<BizUploadScore> list(BizUploadScoreParam bizUploadScoreParam) {

        // 构造条件
        LambdaQueryWrapper<BizUploadScore> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(bizUploadScoreParam)){
            if(ObjectUtil.isNotEmpty(bizUploadScoreParam.getDeviceId())){
                queryWrapper.eq(BizUploadScore::getDeviceId,bizUploadScoreParam.getDeviceId());
            }
        }
        return this.list(queryWrapper);
    }

    @Override
    public boolean add(BizUploadScoreParam bizUploadScoreParam) {

        // 将dto转为实体
        BizUploadScore bizUploadScore = new BizUploadScore();
        BeanUtil.copyProperties(bizUploadScoreParam, bizUploadScore);

        return this.save(bizUploadScore);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizUploadScoreParam bizUploadScoreParam) {
        this.removeById(bizUploadScoreParam.getId());
    }

    @Override
    public void edit(BizUploadScoreParam bizUploadScoreParam) {

        // 根据id查询实体
        BizUploadScore bizUploadScore = this.queryBizUploadScore(bizUploadScoreParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(bizUploadScoreParam, bizUploadScore);

        this.updateById(bizUploadScore);
    }

    @Override
    public BizUploadScore detail(BizUploadScoreParam bizUploadScoreParam) {
        return this.queryBizUploadScore(bizUploadScoreParam);
    }

    @Override
    public PageResult<Map<String,Object>> pageList(BizUploadScoreParam bizUploadScoreParam) {
        String[] list = new String[1];
        String gatewayIds = bizUploadScoreParam.getGatewayIds();
        if(StringUtils.isNotBlank(gatewayIds)){
            list = gatewayIds.split(",");
        }else {
            list[0] = "";
        }
        return new PageResult<>(this.baseMapper.listMap(PageFactory.defaultPage(),bizUploadScoreParam,list));
    }

    @Override
    public List<BizUploadScore> getUploadScoreList(BizUploadScoreParam bizUploadScoreParam) {
        // 构造条件
        LambdaQueryWrapper<BizUploadScore> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(bizUploadScoreParam)){
            if(ObjectUtil.isNotEmpty(bizUploadScoreParam.getDeviceId())){
                queryWrapper.eq(BizUploadScore::getDeviceId,bizUploadScoreParam.getDeviceId());
            }
        }
        queryWrapper.orderByDesc(BizUploadScore::getCreatedAt);
        queryWrapper.last("limit 20");
        return this.list(queryWrapper);
    }


    /**
     * 获取网关上报分数表
     *
     * @author ssy
     * @since 2021/09/14 11:47
     */
    private BizUploadScore queryBizUploadScore(BizUploadScoreParam bizUploadScoreParam) {
        BizUploadScore bizUploadScore = this.getById(bizUploadScoreParam.getId());
        return bizUploadScore;
    }


    /**
     * 根据开始时间、结束时间查询数据
     *
     * @author rdp
     * @date 2022/12/30 11:53
     */
    @Override
    public List<BizUploadScore> getScoreListByTime(Integer deviceId ,String startTime, String endTime) {
        List<BizUploadScore> list = this.baseMapper.getScoreListByTime(deviceId,startTime,endTime);
        return list;
    }

    @Override
    public List<BizUploadScore> getScoresLately(Integer deviceId) {
        return this.baseMapper.getScoresLately(deviceId);
    }
}
